package org.apache.derby.impl.drda;

import org.apache.derby.iapi.jdbc.EnginePreparedStatement;
import org.apache.derby.iapi.jdbc.EngineStatement;

import java.sql.DataTruncation;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 * @author ex_chenzy59
 * @since 2025/3/6
 *
 */
public class DRDAStatement {
    protected EnginePreparedStatement ps;
    private EngineStatement stmt;
    private DRDAResultSet currentDrdaRs;
    private Hashtable<ConsistencyToken, DRDAResultSet> resultSetTable;
    private ArrayList<ConsistencyToken> resultSetKeyList;
    protected ParameterMetaData stmtPmeta;
    private DataTruncation truncationWarnings;
    protected byte [] rslsetflg;
    protected String procName;
    private   int[] outputTypes;
    private int[] outputPrecision;
    private int[] outputScale;
    private DrdaParamState drdaParamState_ = new DrdaParamState();
    protected void close()  throws SQLException
    {
        if (ps != null)
            ps.close();
        if (stmt != null)
            stmt.close();
        currentDrdaRs.close();
        resultSetTable = null;
        resultSetKeyList = null;
        ps = null;
        stmtPmeta = null;
        stmt = null;
        truncationWarnings = null;
        rslsetflg = null;
        procName = null;
        outputTypes = null;
        outputPrecision = null;
        outputScale = null;
        // Clear parameters and release excess storage
        drdaParamState_.clear(true);
    }
    private static class DrdaParamState {
        private byte[] typeLst_ = new byte[10];
        private int[]  lenLst_ = new int[10];
        private int[]  extLst_ = new int[10];
        private EXTDTAReaderInputStream streamedParameter = null;
        private int extLstEnd_ = 0;
        private int typeLstEnd_ = 0;
        protected void clear(boolean trim) {
            streamedParameter = null;
            typeLstEnd_ = 0;
            extLstEnd_ = 0;
            if (trim && typeLst_.length > 10) {
                typeLst_ = new byte[10];
                lenLst_ = new int[10];
                extLst_ = new int[10];
            }
        }
    }
}
